/* Utility scripts for Pardus Sector Tool
 * (C) Yassen 2009
 *
 * pricelist.js
 * Defines functions for getting/setting pricelist information.
 *
 * Functions:
 * void SetBuy(commodity, price, id)
 * void GetBuy(commodity, id)
 * void SetSell(commodity, price, id)
 * void GetSell(commodity, id)
 * void Import(code)
 * Pricelist Export(id)
 *
 */

Global.Pricelists = [];
Global.CurrentPricelist = 0;

Utility.Pricelist.SetBuy = function(commodity, price, id)
{
  if (id == undefined)
    id = Global.CurrentPricelist;

  Global.Pricelists[id].buy[commodity] = price;
}

Utility.Pricelist.GetBuy = function(commodity, id)
{
  if (id == undefined)
    id = Global.CurrentPricelist;

  return Global.Pricelists[id].buy[commodity];
}

Utility.Pricelist.SetSell = function(commodity, price, id)
{
  if (id == undefined)
    id = Global.CurrentPricelist;

  Global.Pricelists[id].sell[commodity] = price;
}

Utility.Pricelist.GetSell = function(commodity, id)
{
  if (id == undefined)
    id = Global.CurrentPricelist;

  return Global.Pricelists[id].sell[commodity];
}

Utility.Pricelist.GetBlank = function(name)
{
  var pricelist = new Pricelist(name, {}, {});

  for (var commodity in Global.Commodities)
  {
    if (!Global.Commodities.hasOwnProperty(commodity))
      continue;

    pricelist.buy[commodity] = 0;
    pricelist.sell[commodity] = 0;
  }

  return pricelist;
}

Utility.Pricelist.Import = function(code)
{
  var regex = new RegExp('"version":([0-9]*)');
  var version = regex.exec(code)[1] * 1;
  return ImportModules[version].ImportPricelist(code);
}

Utility.Pricelist.Export = function(pricelist, id)
{
  if (id == undefined)
    id = Global.CurrentPricelist;
    
  if (pricelist == undefined)
    pricelist = Global.Pricelists[id];

  return ImportModules["Current"].ExportPricelist(pricelist);
}